package com.zjdiepu.www.jsp.tag;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 项目名称 common-jsp <br/> 
 * 类名称 MyUtil <br/>  
 * 类描述 标签工具<br/>    
 * 创建人 xiongxiaotun <br/> 
 * 创建时间 2017年4月26日 下午7:43:03  <br/>   
 * 修改人 xiongxiaotun <br/> 
 * 修改时间 2017年4月26日 下午7:43:03   <br/>  
 * 修改备注   <br/> 
 * @version V1.0   <br/>
 */
public class TagUtil {

	protected Logger logger = LoggerFactory.getLogger(getClass());
	
	public static final String SELECT_FIRST_OPTION = "请选择";
	public static final String SELECT_FIRST_VALUE = "";
	public static final String COMMA_SYMBOL = ",";

	/**
	 * @author xiongxiaotun <br/>
	 * @Title getRadioAndCheckBoxTag  <br/>
	 * @Description 获取 单选框/多选框HTML  <br/>
	 * @param isCheckBox 是否为多选框(true:多选,false:单选)
	 * @param name 名称
	 * @param map 加载项集合
	 * @param selected 如果是checkbox,用英文逗号间隔开
	 * @param tip 前缀名称
	 * @param split 分隔符,默认为 空格
	 * @param prefix 不必填 显示名称前缀加载项
	 * @param suffix 不必填 显示名称后缀加载项
	 * @param others 其他参数值
	 * @param firstSelected 默认选项
	 * @return
	 * @version V1.0  <br/>
	 */
	public static String getRadioAndCheckBoxTag(boolean isCheckBox,
			String name, Map<Object, String> map,
			String selected, String tip, String split, String prefix, String suffix, String others, boolean firstSelected) {
		int mapsize = 0;

		if (StringUtils.isBlank(name)) {
			if (isCheckBox) {
				name = "c1";
			} else {
				name = "r1";
			}
		}
		if (StringUtils.isBlank(split)) {
			split = "&nbsp;";
		}
		if (map != null) {
			mapsize = map.size();
		}
		StringBuilder html = new StringBuilder();
		if (StringUtils.isNotBlank(tip)) {
			html.append(tip);
			html.append("：");
		}
		if (mapsize > 0) {
			String type = isCheckBox ? "checkbox" : "radio";
			Iterator<Map.Entry<Object, String>> iterator = map.entrySet()
					.iterator();
			Object key = null;
			Object value = null;
			Map.Entry<Object, String> entry = null;

			boolean selectedExists = false;
			if (StringUtils.isNotBlank(selected)) {
				selectedExists = true;
			}

			while (iterator.hasNext()) {
				entry = iterator.next();
				key = entry.getKey();
				value = entry.getValue();
				html.append("<input name=\"");
				html.append(name);
				html.append("\" type=\"");
				html.append(type + "\" ");

				if (StringUtils.isNotBlank(key.toString())) {
					html.append(" value=\"");
					html.append(key);
					html.append("\" ");
				}

				if (isCheckBox) {
					if (selectedExists
							&& (selected + ",").indexOf(key.toString() + ",") >= 0) {
						html.append("checked");
					}
				} else {
					if (StringUtils.isNotBlank(selected)
							&& selected.equals(key.toString())) {
						html.append("checked");
					} else if (firstSelected && StringUtils.isBlank(selected)) {
							html.append("checked");
							firstSelected = false;
					}
				}
				
				if (StringUtils.isNotBlank(others)) {
					html.append(" " + others);
				}
				html.append("/>");
				if(StringUtils.isNotBlank(prefix) && StringUtils.isNotBlank(suffix)){
					html.append(prefix);
					html.append(value);
					html.append(suffix);
				} else {
					html.append(value);
				}
				html.append(split);
			}
		}
		return html.toString();
	}
	
	public static String getSelectTag(String id, String name, boolean firstFlag, boolean multiple, String firstoption,String firstvalue,String title
			,String select,Map<Object, Object> map,String other){
		
		StringBuilder html = new StringBuilder();
		
		title = StringUtils.isBlank(title) ? "" : title;
		id = StringUtils.isBlank(id) ? "" : id;
		name = StringUtils.isBlank(name) ? "" : name;
		select = StringUtils.isBlank(select) ? "" : select;
		if(StringUtils.isNotBlank(title)){
			html.append("<span>").append(title).append("</span>");
		}
		if(StringUtils.isBlank(other)){
			html.append("<select id='").append(id).append("' name='").append(name);
			if(multiple) {
				html.append("' multiple='").append(multiple);
			}
			html.append("'>");
		}else{
			html.append("<select id='").append(id).append("' name='").append(name).append("' ");
			if(multiple) {
				html.append(" multiple='").append(multiple).append("' ");
			}
			html.append(other);
			html.append(">");
		}
		
		if(firstFlag){
			firstoption = StringUtils.isBlank(firstoption) ? SELECT_FIRST_OPTION : firstoption;
			firstvalue = StringUtils.isBlank(firstvalue) ? SELECT_FIRST_VALUE : firstvalue;
			html.append("<option value='").append(firstvalue).append("'>").append(firstoption).append("</option>");
		}
		
		if(null != map && map.size() > 0){
			Iterator<Object> it = map.keySet().iterator();
			// 单选
			if(!select.contains(COMMA_SYMBOL)){
				while(it.hasNext()){
					Object key = it.next();
					if(key.toString().equals(select)){
						html.append("<option selected='selected' value='").append(key).append("'>").append(map.get(key)).append("</option>");
					}else{
						html.append("<option value='").append(key).append("'>").append(map.get(key)).append("</option>");
					}
				}
			}
			// 多选
			else{
				String[] ids = select.split(COMMA_SYMBOL);
				List<String> idList = new ArrayList<String>();
				for(String temp : ids){
					if(!StringUtils.isBlank(temp) && !temp.equals("0")){
						idList.add(temp);
					}
				}
				while(it.hasNext()){
					Object key = it.next();
					if(idList.contains(key.toString())){
						html.append("<option selected='selected' value='").append(key).append("'>").append(map.get(key)).append("</option>");
					}else{
						html.append("<option value='").append(key).append("'>").append(map.get(key)).append("</option>");
					}
				}
			}
		}
		html.append("</select>");
		return html.toString();
	}
}
